<?php

use Workerman\Protocols\Http\Response;
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;

require_once __DIR__ . '/vendor/autoload.php';

// 创建一个 Worker 监听 9501 端口，使用 http 协议
$worker = new Worker("http://0.0.0.0:9501");

// 启动 4 个进程对外提供服务
$worker->count = 4;

$worker->name = "http";

// Worker::$stdoutFile = './stdout.log';

// 接收到浏览器发送的数据时回复 "hello world"
$worker->onMessage = function(TcpConnection $connection, Request $request)
{
//    $response = new Response(200, [
//        'Content-Type' => 'text/html',
//        'X-Header-One' => 'Header Value'
//    ], 'this is body');
//    $response->cookie('name', 'tom');
//    $connection->send($response);
    echo "你好\n";

    $session = $request->session();
    $session->set('name', 'tome');
    $connection->send($session->get('name'));
};

$socketName = $worker->getSocketName();
echo "Worker started: https://$socketName\n";

Worker::runAll();
